<template>
  <div id="webgl"></div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader.js";
import { GUI } from "three/addons/libs/lil-gui.module.min.js";
import * as THREE from "three";
import earth_img from "../assets/PerspectiveCamera.png";
import poly_haven_studio_4k_hdr from "../assets/poly_haven_studio_4k.hdr";

// 创建3D场景对象Scene
const scene = new THREE.Scene();
// 创建长方体
const boxGeometry = new THREE.BoxGeometry(1, 1, 100);
const boxMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const box = new THREE.Mesh(boxGeometry, boxMaterial);
scene.add(box);

// 创建场景fog
scene.fog = new THREE.Fog(0x999999, 0.1, 50); // 雾的颜色，雾的浓度，雾的远近
// 创建场景指数fog
// scene.fog = new THREE.FogExp2(0x999999, 0.1); // 雾的颜色，雾的浓度
scene.background = new THREE.Color(0x999999); // 背景颜色

// 定义threejs输出画布的尺寸(单位:像素px)
const width = window.innerWidth; //宽度
const height = window.innerHeight; //高度
// 实例化一个透视投影相机对象
const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
//相机在Three.js三维坐标系中的位置
// 根据需要设置相机位置具体值
camera.position.set(0, 0, 10);
//相机观察目标指向Threejs 3D空间中某个位置
camera.lookAt(box.position); //指向mesh对应的位置

// AxesHelper：辅助观察的坐标系
const axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);

// 创建渲染器对象
const renderer = new THREE.WebGLRenderer();

renderer.setSize(width, height); //设置three.js渲染区域的尺寸(像素px)
renderer.setClearColor(0xffffff, 1);
renderer.render(scene, camera);

// 设置相机控件轨道控制器OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
// 如果OrbitControls改变了相机参数，重新调用渲染器渲染三维场景
controls.addEventListener("change", function () {
  renderer.render(scene, camera); //执行渲染操作
}); //监听鼠标、键盘事件

function render() {
  renderer.render(scene, camera); //执行渲染操作
  requestAnimationFrame(render);
}
render();

onMounted(() => {
  document.getElementById("webgl").appendChild(renderer.domElement);
});
</script>
